﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FBWMS;

namespace FBWMS
{
    public partial class uc_textbox_ReadOnly : UserControl
    {
        private Color m_GraphicsBackColor = Color.Transparent;

        public uc_textbox_ReadOnly()
        {
            // 透過に対応
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            // リサイズ時に再描画
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            //' ダブルバッファを使用してちらつきを抑える
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            // BackColor のデフォルト値を透過
            base.BackColor = Color.Transparent;

            InitializeComponent();
        }

        [Category("Graphics"), Description("角の丸いラベルの背景色"), DefaultValue(typeof(Color), "Red")]
        public Color GraphicsBackColor
        {
            get
            {
                return this.m_GraphicsBackColor;
            }
            set
            {
                this.m_GraphicsBackColor = value;
                this.Refresh();
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Brush GraphicsBKBrush;
            GraphicsBKBrush = new SolidBrush(m_GraphicsBackColor);
            int iPenWidth = 3;
            int iInsetWidth = iPenWidth / 2 + iPenWidth % 2;
            Rectangle rc = new Rectangle(iInsetWidth, iInsetWidth, this.Width - iInsetWidth * 2, this.Height - iInsetWidth * 2);
            Size size = new Size(this.Width / 5, this.Height / 5);
            // 描画
            FillRoundedRectangle(e.Graphics, new Pen(m_GraphicsBackColor, iPenWidth), new SolidBrush(m_GraphicsBackColor), rc, size);
        }
        //OnPaint
        private void FillRoundedRectangle(Graphics g, Pen pen, Brush backBrush, Rectangle rect, Size size)
        {
            System.Drawing.Drawing2D.SmoothingMode sm;
            System.Drawing.Drawing2D.PixelOffsetMode pom;
            System.Drawing.Drawing2D.GraphicsPath graphicPath;
            sm = g.SmoothingMode;
            pom = g.PixelOffsetMode;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            graphicPath = new System.Drawing.Drawing2D.GraphicsPath();
            graphicPath.FillMode = System.Drawing.Drawing2D.FillMode.Winding;
            graphicPath.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270, 90);
            graphicPath.AddArc(rect.Right - size.Width, rect.Bottom - size.Height, size.Width, size.Height, 0, 90);
            graphicPath.AddArc(rect.Left, rect.Bottom - size.Height, size.Width, size.Height, 90, 90);
            graphicPath.AddArc(rect.Left, rect.Top, size.Width, size.Height, 180, 90);
            graphicPath.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270, 90);
            g.DrawPath(pen, graphicPath);
            g.SmoothingMode = sm;
            g.PixelOffsetMode = pom;
        }

        //ユーザーコントロールにTextのプロパティを付加する。
        public new string Text { get { return txt_ReadOnly.Text; } set { txt_ReadOnly.Text = value; } }

        //ユーザーコントロールにTextのTextChangedのイベントを付加する。
        public new event EventHandler TextChanged { add { txt_ReadOnly.TextChanged += value; } remove { } }
    }
}
